package scauCmiScaffolding.compiled;

import org.eclipse.jet.JET2Context;
import org.eclipse.jet.JET2Template;
import org.eclipse.jet.JET2Writer;
import org.eclipse.jet.taglib.RuntimeTagElement;
import org.eclipse.jet.taglib.TagInfo;

public class _jet_cmiDetachedCriteriaUtil_0 implements JET2Template {
    private static final String _jetns_c = "org.eclipse.jet.controlTags"; //$NON-NLS-1$
    private static final String _jetns_java = "org.eclipse.jet.javaTags"; //$NON-NLS-1$

    public _jet_cmiDetachedCriteriaUtil_0() {
        super();
    }

    private static final String NL = System.getProperty("line.separator"); //$NON-NLS-1$
    
    private static final TagInfo _td_c_get_1_9 = new TagInfo("c:get", //$NON-NLS-1$
            1, 9,
            new String[] {
                "select", //$NON-NLS-1$
            },
            new String[] {
                "$packageName", //$NON-NLS-1$
            } );
    private static final TagInfo _td_c_get_9_8 = new TagInfo("c:get", //$NON-NLS-1$
            9, 8,
            new String[] {
                "select", //$NON-NLS-1$
            },
            new String[] {
                "$packageName", //$NON-NLS-1$
            } );
    private static final TagInfo _td_java_format_10_1 = new TagInfo("java:format", //$NON-NLS-1$
            10, 1,
            new String[] {
            },
            new String[] {
            } );

    public void generate(final JET2Context context, final JET2Writer __out) {
        JET2Writer out = __out;
        out.write("package ");  //$NON-NLS-1$        
        RuntimeTagElement _jettag_c_get_1_9 = context.getTagFactory().createRuntimeTag(_jetns_c, "get", "c:get", _td_c_get_1_9); //$NON-NLS-1$ //$NON-NLS-2$
        _jettag_c_get_1_9.setRuntimeParent(null);
        _jettag_c_get_1_9.setTagInfo(_td_c_get_1_9);
        _jettag_c_get_1_9.doStart(context, out);
        _jettag_c_get_1_9.doEnd();
        out.write(".util;");  //$NON-NLS-1$        
        out.write(NL);         
        out.write(NL);         
        out.write("import java.util.Iterator;");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("import java.util.List;");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("import org.hibernate.criterion.DetachedCriteria;");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("import org.hibernate.criterion.Restrictions;");  //$NON-NLS-1$        
        out.write(NL);         
        out.write("import org.springframework.transaction.annotation.Transactional;");  //$NON-NLS-1$        
        out.write(NL);         
        out.write(NL);         
        out.write("import ");  //$NON-NLS-1$        
        RuntimeTagElement _jettag_c_get_9_8 = context.getTagFactory().createRuntimeTag(_jetns_c, "get", "c:get", _td_c_get_9_8); //$NON-NLS-1$ //$NON-NLS-2$
        _jettag_c_get_9_8.setRuntimeParent(null);
        _jettag_c_get_9_8.setTagInfo(_td_c_get_9_8);
        _jettag_c_get_9_8.doStart(context, out);
        _jettag_c_get_9_8.doEnd();
        out.write(".base.bean.CmiPagination;");  //$NON-NLS-1$        
        out.write(NL);         
        RuntimeTagElement _jettag_java_format_10_1 = context.getTagFactory().createRuntimeTag(_jetns_java, "format", "java:format", _td_java_format_10_1); //$NON-NLS-1$ //$NON-NLS-2$
        _jettag_java_format_10_1.setRuntimeParent(null);
        _jettag_java_format_10_1.setTagInfo(_td_java_format_10_1);
        _jettag_java_format_10_1.doStart(context, out);
        JET2Writer _jettag_java_format_10_1_saved_out = out;
        while (_jettag_java_format_10_1.okToProcessBody()) {
            out = out.newNestedContentWriter();
            out.write(NL);         
            out.write("\t// 离线查询，");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("public class CmiDetachedCriteriaUtil {");  //$NON-NLS-1$        
            out.write(NL);         
            out.write(NL);         
            out.write("\t");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("\t//依赖于分页类对象Page的组合条件进行DetachedCriteria语句的构造");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("\tpublic static DetachedCriteria createDetachedCriteria(CmiPagination cmiPagination,Class entity) {");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("\t\tDetachedCriteria detachedCriteria = DetachedCriteria.forClass(entity);");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("\t\tIterator<String> combinedSelectConditionsIterator = cmiPagination.getCombinedSelectConditions().iterator();");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("\t\tIterator<String> combinedConditionsIterator = cmiPagination.getCombinedConditions().iterator();");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("\t\tString selectCondition;");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("\t\tString condition;");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("\t\twhile(combinedConditionsIterator.hasNext()) {");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("\t\t\tselectCondition = combinedSelectConditionsIterator.next();");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("\t\t\tselectCondition = selectCondition.substring(0, 1).toLowerCase() + selectCondition.substring(1);");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("\t\t\tcondition = combinedConditionsIterator.next().trim();");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("\t\t\tif(condition.equals(\"\"))  continue;");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("\t\t\t");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("//\t\t\t如果是String类型，使用模糊查巡，如果是其他类型，就使用精确查询。");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("\t\t\ttry {");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("\t\t\t\tif(entity.getDeclaredField(selectCondition).getType().getName().equals(\"java.lang.String\")) {");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("\t\t\t\t\tdetachedCriteria.add(Restrictions.sqlRestriction(selectCondition + \" like \\'%\" + condition + \"%\\'\"));");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("\t\t\t\t}else {");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("\t\t\t\t\tdetachedCriteria.add(Restrictions.sqlRestriction(selectCondition + \" = \\'\" + condition + \"\\'\"));");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("\t\t\t\t}");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("\t\t\t} catch (NoSuchFieldException | SecurityException e) {");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("\t\t\t\t// TODO Auto-generated catch block");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("\t\t\t\te.printStackTrace();");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("\t\t\t}");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("\t\t}");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("\t\treturn detachedCriteria;");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("\t}");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("\t");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("\t");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("\t");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("\tpublic static DetachedCriteria createDetachedCriteriaInRange(CmiPagination cmiPagination,Class entity) {");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("\t\tDetachedCriteria detachedCriteria = DetachedCriteria.forClass(entity);");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("\t\tIterator<String> combinedSelectConditionsIterator = cmiPagination.getCombinedSelectConditions().iterator();");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("\t\tIterator<String> combinedConditionsIterator = cmiPagination.getCombinedConditions().iterator();");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("\t\tString selectCondition;");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("\t\tString condition;");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("\t\tdetachedCriteria.add(Restrictions.in(\"id\", cmiPagination.getEventIds()));");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("\t\twhile(combinedConditionsIterator.hasNext()) {");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("\t\t\tselectCondition = combinedSelectConditionsIterator.next();");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("\t\t\tselectCondition = selectCondition.substring(0, 1).toLowerCase() + selectCondition.substring(1);");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("\t\t\tcondition = combinedConditionsIterator.next().trim();");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("\t\t\tif(condition.equals(\"\"))  continue;");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("\t\t\t");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("//\t\t\t如果是String类型，使用模糊查巡，如果是其他类型，就使用精确查询。");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("\t\t\ttry {");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("\t\t\t\tif(entity.getDeclaredField(selectCondition).getType().getName().equals(\"java.lang.String\")) {");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("\t\t\t\t\tdetachedCriteria.add(Restrictions.sqlRestriction(selectCondition + \" like \\'%\" + condition + \"%\\'\"));");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("\t\t\t\t}else {");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("\t\t\t\t\tdetachedCriteria.add(Restrictions.sqlRestriction(selectCondition + \" = \\'\" + condition + \"\\'\"));");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("\t\t\t\t}");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("\t\t\t} catch (NoSuchFieldException | SecurityException e) {");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("\t\t\t\t// TODO Auto-generated catch block");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("\t\t\t\te.printStackTrace();");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("\t\t\t}");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("\t\t}");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("\t\treturn detachedCriteria;");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("\t}");  //$NON-NLS-1$        
            out.write(NL);         
            out.write("}");  //$NON-NLS-1$        
            out.write(NL);         
            out.write(NL);         
            _jettag_java_format_10_1.handleBodyContent(out);
        }
        out = _jettag_java_format_10_1_saved_out;
        _jettag_java_format_10_1.doEnd();
    }
}
